<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            class="eidt-el-form"
          >
            <el-form-item label="姓名" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="手机" prop="phone">
              <el-input v-model="ruleForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="车牌" prop="carNo">
              <el-input v-model="ruleForm.carNo" ></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import checkPhone from "../../common/checkPhone";

export default {
  name: "editvihicel",
  data() {
    return {
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        phone: [{ required: true, validator: checkPhone, trigger: "blur" }],
        carNo: [{ required: true, message: "车牌不能为空", trigger: "blur" }]
      },
      ruleForm: {
        name: "",
        phone:"",
        carNo:"",
        projectId: ""
      },
      projectCar_Id: "",
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created() {
    var project_Id = localStorage.getItem("project_Id");
    this.projectId = project_Id;
  },
  watch: {
    editData: function() {
      const item = this.editData;
      this.ruleForm = {
        name: item.name,
        phone: item.phone,
        carNo: item.carNo
      };
      this.projectCar_Id = item.projectCarId;
    }
  },
  methods: {
    saveClick(ruleForm) {
      this.ruleForm = {
        name: this.ruleForm.name,
        phone: this.ruleForm.phone,
        carNo: this.ruleForm.carNo,
        projectId: this.projectId
      };
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.projectCar_Id == "" || this.projectCar_Id == undefined) {
            this.$axios.post("/ProjectCar/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
                this.$refs[ruleForm].resetFields();
              } else {
                this.$message.error("保存失败！");
              }
            });
          } else {
            this.$axios
              .put("/ProjectCar/" + this.projectCar_Id + "", this.ruleForm)
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                  this.$refs[ruleForm].resetFields();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$emit("backFunStu");
      this.projectCar_Id = "";
    }
  }
};
</script>